package com.zcr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zcr.pojo.Jinhuorecords;
import com.zcr.pojo.Sell;
import com.zcr.pojo.Sellrecords;
import com.zcr.pojo.Store;
import com.zcr.service.SellService;
import com.zcr.service.SellrecordsService;
import com.zcr.service.StoreService;
import com.zcr.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * @version 1.0
 * @author： 张诚
 * @date： 2021-03-25 14:12
 */
@Controller
@RequestMapping("/lingshou")
public class LingShouController {

    @Resource
    private SellService sellService;

    @Resource
    private StoreService storeService;

    @Resource
    private SellrecordsService sellrecordsService;

    @GetMapping("/index")
    public String login(){
        return "lingshou";
    }

    @PostMapping("/add")
    public Result add(Sell sell){

        int zongjia=Integer.parseInt(sell.getPrice())*Integer.parseInt(sell.getSum());

        try {
            Sell se = new Sell();
            se.setDrugname(sell.getDrugname());
            se.setChangshang(sell.getChangshang());
            se.setGuige(sell.getGuige());
            se.setUnit(sell.getUnit());
            se.setPrice(sell.getPrice());
            se.setDate(sell.getDate());
            se.setPihao(sell.getPihao());
            se.setBeizhu(sell.getBeizhu());

            int sysl=Integer.parseInt(sell.getCount())-Integer.parseInt(sell.getSum());

            se.setCount(String.valueOf(sysl));

            se.setUnit(sell.getUnit());
            se.setGuige(sell.getGuige());
            se.setAmount(sell.getSum());

            se.setSum(String.valueOf(zongjia));
            sellService.add(se);
            QueryWrapper<Store> storeQueryWrapper=new QueryWrapper<>();
            storeQueryWrapper.eq("drugname",sell.getDrugname());
            storeQueryWrapper.eq("changshang",sell.getChangshang());
            Store list=storeService.getOne(storeQueryWrapper);
            int shuliang=Integer.parseInt(list.getCount())-Integer.parseInt(sell.getSum());
            list.setCount(String.valueOf(shuliang));
            storeService.update(list);
            Sellrecords sellrecords = new Sellrecords();

            sellrecords.setDrugname(sell.getDrugname());
            sellrecords.setChangshang(sell.getChangshang());
            sellrecords.setGuige(sell.getGuige());
            sellrecords.setUnit(sell.getUnit());
            sellrecords.setPrice(sell.getPrice());
            sellrecords.setDate(sell.getDate());
            sellrecords.setPihao(sell.getPihao());
            sellrecords.setBeizhu(sell.getBeizhu());
            sellrecords.setUnit(sell.getUnit());
            sellrecords.setGuige(sell.getGuige());
            sellrecords.setAmount(sell.getSum());
            sellrecords.setSum(String.valueOf(zongjia));
            sellrecords.setSelltime(String.valueOf(LocalDate.now()));

            sellrecordsService.add(sellrecords);

            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.sucess("添加失败");
        }
    }

}
